#!/bin/bash

# iOS 模拟器停止脚本
# 用于关闭正在运行的 iOS 模拟器

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查 Xcode 是否安装
check_xcode() {
    if ! command -v xcrun &> /dev/null; then
        print_error "未找到 Xcode 命令行工具"
        exit 1
    fi
}

# 检查是否有模拟器在运行
check_simulator_running() {
    xcrun simctl list devices | grep "Booted" > /dev/null 2>&1
    return $?
}

# 停止所有模拟器
stop_all_simulators() {
    print_info "正在关闭所有模拟器..."
    xcrun simctl shutdown all
    
    if [ $? -eq 0 ]; then
        print_success "所有模拟器已关闭"
        return 0
    else
        print_error "关闭模拟器时出错"
        return 1
    fi
}

# 停止指定的模拟器
stop_simulator() {
    local device_udid=$1
    local device_name=$2
    
    print_info "正在关闭模拟器: $device_name"
    xcrun simctl shutdown "$device_udid"
    
    if [ $? -eq 0 ]; then
        print_success "模拟器 $device_name 已关闭"
        return 0
    else
        print_error "关闭模拟器失败"
        return 1
    fi
}

# 擦除模拟器数据
erase_simulator() {
    local device_udid=$1
    local device_name=$2
    
    print_warning "即将擦除模拟器数据: $device_name"
    read -p "确定要擦除所有数据吗？(y/N): " answer
    
    if [[ $answer =~ ^[Yy]$ ]]; then
        print_info "正在擦除数据..."
        xcrun simctl erase "$device_udid"
        
        if [ $? -eq 0 ]; then
            print_success "数据已擦除"
        else
            print_error "擦除数据失败"
        fi
    else
        print_info "取消擦除操作"
    fi
}

# 主函数
main() {
    echo "=================================="
    echo "  iOS 模拟器停止脚本"
    echo "=================================="
    echo ""
    
    # 检查 Xcode
    check_xcode
    
    if ! check_simulator_running; then
        print_warning "没有正在运行的模拟器"
        
        # 检查模拟器应用是否在运行
        if pgrep -x "Simulator" > /dev/null; then
            print_info "但检测到模拟器应用在运行"
            read -p "是否要退出模拟器应用？(y/N): " answer
            if [[ $answer =~ ^[Yy]$ ]]; then
                killall Simulator
                print_success "模拟器应用已退出"
            fi
        fi
        exit 0
    fi
    
    # 显示正在运行的模拟器
    print_info "正在运行的模拟器："
    echo ""
    xcrun simctl list devices | grep "Booted"
    echo ""
    
    # 获取运行中的模拟器列表
    local booted_devices=()
    local booted_udids=()
    
    while IFS= read -r line; do
        if [ -n "$line" ]; then
            booted_devices+=("$line")
            # 提取 UDID (匹配标准 UUID 格式)
            udid=$(echo "$line" | grep -oE '[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}')
            booted_udids+=("$udid")
        fi
    done < <(xcrun simctl list devices | grep "Booted" | sed 's/^[[:space:]]*//')
    
    if [ ${#booted_devices[@]} -eq 0 ]; then
        print_warning "没有正在运行的模拟器"
        exit 0
    fi
    
    if [ ${#booted_devices[@]} -eq 1 ]; then
        # 只有一个模拟器
        device_name=$(echo "${booted_devices[0]}" | sed 's/\(.*\) (.*/\1/' | xargs)
        device_udid="${booted_udids[0]}"
        
        echo "请选择操作："
        echo "1) 关闭模拟器"
        echo "2) 关闭并擦除数据"
        echo "3) 取消"
        read -p "请输入选项 (1/2/3): " choice
        
        case $choice in
            1)
                stop_simulator "$device_udid" "$device_name"
                ;;
            2)
                stop_simulator "$device_udid" "$device_name"
                erase_simulator "$device_udid" "$device_name"
                ;;
            3)
                print_info "已取消"
                exit 0
                ;;
            *)
                print_error "无效的选项"
                exit 1
                ;;
        esac
    else
        # 多个模拟器
        echo "请选择操作："
        echo "1) 关闭所有模拟器"
        echo "2) 选择特定模拟器关闭"
        echo "3) 取消"
        read -p "请输入选项 (1/2/3): " choice
        
        case $choice in
            1)
                stop_all_simulators
                ;;
            2)
                print_info "请选择要关闭的模拟器:"
                echo ""
                for i in "${!booted_devices[@]}"; do
                    echo "  $((i+1))) ${booted_devices[$i]}"
                done
                echo ""
                read -p "请输入编号: " sim_choice
                
                sim_index=$((sim_choice-1))
                if [ $sim_index -lt 0 ] || [ $sim_index -ge ${#booted_devices[@]} ]; then
                    print_error "无效的选择"
                    exit 1
                fi
                
                device_name=$(echo "${booted_devices[$sim_index]}" | sed 's/\(.*\) (.*/\1/' | xargs)
                device_udid="${booted_udids[$sim_index]}"
                
                stop_simulator "$device_udid" "$device_name"
                
                read -p "是否要擦除此模拟器的数据？(y/N): " erase_answer
                if [[ $erase_answer =~ ^[Yy]$ ]]; then
                    erase_simulator "$device_udid" "$device_name"
                fi
                ;;
            3)
                print_info "已取消"
                exit 0
                ;;
            *)
                print_error "无效的选项"
                exit 1
                ;;
        esac
    fi
    
    # 询问是否要退出模拟器应用
    if pgrep -x "Simulator" > /dev/null; then
        echo ""
        read -p "是否要退出模拟器应用？(y/N): " answer
        if [[ $answer =~ ^[Yy]$ ]]; then
            killall Simulator
            print_success "模拟器应用已退出"
        fi
    fi
}

# 运行主函数
main

